# -*- coding: utf-8 -*-

'''eof
name:企业类型
code:EG02_EnterpriseType
tableName:
columnName:
groups:企业通用模块
dependencies:EG_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import pandas as pd
import json
from datetime import datetime


def FormatData(tableName):
    try:
        eg = EG_DSJ.get("data").get("R1103V3")[0].get(tableName)
        return eg if isinstance(eg, list) else [{}]
    except:
        return [{}]


def convertCompanyType(type):
    bool1 = (u"分" in type) or (u"经营单位" in type) or (u"办事处" in type) or (u"办事机构" in type) or (u"机构" in type)
    bool2 = (u"外国（地区）企业在中国境内从事经营活动" in type) or (u"外国（地区）企业（其他）" in type)
    if bool1 or bool2:
        return u"分支机构等"

    bool3 = (u"外" in type) and (u"独资" in type) and (u"投资" not in type)
    bool4 = (u"港" in type) and (u"独资" in type)
    bool5 = (u"澳" in type) and (u"独资" in type)
    bool6 = (u"台" in type) and (u"独资" in type)
    if bool3 or bool4 or bool5 or bool6:
        return u"外商独资"

    if u"个人独资企业" in type:
        return u"个人独资企业"

    if u"个" in type:
        return u"个体工商户"

    if u"股份有限公司" in type:
        return u"股份有限公司"

    if ((u"有限责任公司" in type) and (u"独资" in type)) or (u"一人有限责任公司" in type) or (u"有限公司（自然人独资）" in type):
        return u"一人有限责任公司"

    if u"有限责任公司" in type:
        return u"有限责任公司"

    if u"合伙" in type:
        return u"合伙企业"

    if (u"企业" in type) or (u"内资公司" in type) or (u"公司" in type) or (u"非公司" in type) or (u"集团" in type):
        return u"其他"

    return u'缺失值'


def getEG02():
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    try:
        basic_list = FormatData("basicList")
        if basic_list == [{}]:
            return u'缺失值'
        else:
            if len(basic_list) >= 1:
                for i in basic_list:
                    enterprise_type = i.get("enterpriseType")
                    result_type = convertCompanyType(enterprise_type)
                    if result_type not in null_type_list:
                        if result_type in [u"股份有限公司", u"一人有限责任公司", u"有限责任公司"]:
                            return result_type
                        else:
                            return u'其他'
                    else:
                        return u'缺失值'
                return u'缺失值'
            else:
                return u'缺失值'
    except:
        return u'缺失值'


if __name__ == "__main__":
    file_obj = open(r'../data/EG/EG02_EnterpriseType/EG_DSJ.json', 'r')
    content = file_obj.read()
    EG_DSJ = json.loads(content, strict=False)
    aa = getEG02()
    print aa

result = getEG02()

